package lista.ex02;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/ex02/livroVisitasServlet")
public class LivroVisitasServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;
	
	private List<String> livroVisitas = new ArrayList<String>();
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		
		PrintWriter out;
		try {
			out = response.getWriter();
			response.setContentType("text/html");
			
			out.println("<html><body>");
			out.println("<h1>Livro de Visitas</h1>");
			
			out.println("<form action=\"livroVisitasServlet\" method=\"get\">");
			out.println("Nome: <input type=\"text\" name=\"nome\"/>");
			out.println("<br/>");
			out.println("Recado: <input type=\"text\" name=\"recado\"/>");
			out.println("<br/>");
			out.println("<input type=\"submit\" value=\"Registrar Visita\"/>");
						
			out.println("</form>");
			
			String nome = request.getParameter("nome");
			String recado = request.getParameter("recado");
			
			if(nome != null && recado != null){
				livroVisitas.add(nome+" - "+recado);
			}
			
			int i = 1;
			for (String visita : livroVisitas) {
				out.println(i +" - " +visita);
				out.println("<br/>");
				i++;
			}
			
			
			out.println("</body></html>");
			out.close();
			
		}catch(IOException e){
			e.printStackTrace();
		}
		
	}
	

}
